package com.example.zwh.computershop.handleDataClient;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;

import com.example.zwh.computershop.R;
import com.example.zwh.computershop.sqlModel.Cart;
import com.example.zwh.computershop.sqlModel.Order;
import com.example.zwh.computershop.sqlModel.Product;
import com.example.zwh.computershop.sqlModel.User;
import com.google.gson.Gson;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;

//客户端接受数据
public class handleData {
	private static Gson gson = new Gson();

	//获取图片
	public Bitmap getBitmap(String url) {
		URL imgUrl = null;
		Bitmap bitmap = null;
		try {
			imgUrl = new URL(url);
			HttpURLConnection conn = (HttpURLConnection) imgUrl
					.openConnection();
			conn.setDoInput(true);
			conn.connect();
			InputStream is = conn.getInputStream();
			bitmap = BitmapFactory.decodeStream(is);
			is.close();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return bitmap;
	}

	public static int handleLogin(String un, String ps, User user) {

		String url = "http://39.108.12.42:8080/anServer/login?un=" + un + "&ps=" + ps;
		String content = JsonUtil.getWebTxt(url);

		//System.out.println(content);

		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			int state = obj.getInt("state");
			if (state == 2) {
				//登录成功时传参数
				String u = obj.getString("user");
				User usert = (User) gson.fromJson(u, User.class);
				//User usert = (User) JsonUtil.getObject(obj, "user");
				//usert.printInfo();
				//拷贝
				user.setUser(usert);

			}
			return state;
		}
	}

	public static int handleRegister(User user) {
		
		String un = user.getUsername();
		String ps = user.getPassword();
		String em = user.getEmail();
		String ph = "no";
		
		String url = "http://39.108.12.42:8080/anServer/register?un=" + un + "&ps=" + ps + "&em=" + em + "&ph=" + ph;
		//System.out.println(url);
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			int state = obj.getInt("state");
			if (state == 2) {
				String u = obj.getString("user");
				User usert = (User)gson.fromJson(u, User.class);
				user.setUser(usert);
			}
			return state;
		}
	}
	
	public static int handleInfo(User user) {
		String un = user.getUsername();
		String ps = user.getPassword();
		String em = user.getEmail();
		String ph = user.getPhone();
		int ro = user.getRole();
		String hd = user.getHeadPortraitUrl();
		int uid = user.getId();
		//http://39.108.12.42:8080/anServer/alterInfoServlet?un=zw&ps=z&em=1231&ph=1&hd=1&ro=1&uid=80
		String url = "http://39.108.12.42:8080/anServer/alterInfoServlet?un=" + un + "&ps=" + ps + "&em=" + em + "&ph=" + ph + "&hd=" + hd + "&ro=" + ro + "&uid=" + uid;
		System.out.println(url);
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{' || (content.charAt(0) == '{' && content.charAt(1) == '}')) {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			int state = obj.getInt("state");
			return state;
		}
	}
	
	public static int getOrd(ArrayList<Order> ora, int uid) {
		String url = "http://39.108.12.42:8080/anServer/getOrdServlet?uid="+uid;
		String content = JsonUtil.getWebTxt(url);
		
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			if (state == 3) {
				JSONArray jora = obj.getJSONArray("orders");
				for (int i = 0; i < jora.size(); i++) {
					Order or = (Order)gson.fromJson(jora.get(i).toString(), Order.class);
					ora.add(or);
				}
			}
			return state;
		}
	}
	
	public static int getCart(ArrayList<Cart> cara, int uid) {
		String url = "http://39.108.12.42:8080/anServer/getCartServlet?uid="+uid;
		String content = JsonUtil.getWebTxt(url);
		
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			if (state == 3) {
				JSONArray jora = obj.getJSONArray("carts");
				for (int i = 0; i < jora.size(); i++) {
					Cart p = (Cart) gson.fromJson(jora.get(i).toString(), Cart.class);
					cara.add(p);
				}
			}
			return state;
		}
	}
	
	public static int addCart(Cart ca) {
		String url = "http://39.108.12.42:8080/anServer/inCartServlet?uid="+ca.getId()+"&pid="+ca.getProductId()+"&anum="+ca.getAddNum();
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			int state = obj.getInt("state");
			return state;
		}
	}
	
	public static int alterCart(Cart ca) {
		String url = "http://39.108.12.42:8080/anServer/alterCartServlet?uid="+ca.getId()+"&pid="+ca.getProductId()+"&anum="+ca.getAddNum();
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			return state;
		}
	}
	
	public static int dropCart(Cart ca) {
		String url = "http://39.108.12.42:8080/anServer/dropCartServlet?uid="+ca.getId()+"&pid="+ca.getProductId();
		String content = JsonUtil.getWebTxt(url);
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			
			int state = obj.getInt("state");
			return state;
		}
	}
	
	public static int getPro(ArrayList<Product> proa) {
		String url = "http://39.108.12.42:8080/anServer/getProServlet";
		String content = JsonUtil.getWebTxt(url);
		
		if (content.charAt(0) != '{') {
			//联网失败
			return 5;
		}else {
			JSONObject obj = new JSONObject().fromObject(content);
			int state = obj.getInt("state");
			JSONArray jora = obj.getJSONArray("products");
			for (int i = 0; i < jora.size(); i++) {
				Product p = (Product) gson.fromJson(jora.get(i).toString(), Product.class);
				proa.add(p);
			}
			return state;
		}
	}
	
	public static int getState(JSONObject obj) {
		// 获取要转的对象数据
		int state = obj.getInt("state");
		return state;
	}


	//根据图片文件名获取id
	public static int getResourceByReflect(String imageName){
		String tempName[] = imageName.split("\\.");
		//p001.jpg
		Class drawable  =  R.drawable.class;
		Field field = null;
		int r_id ;
		try {
			System.out.println("********imageName" + tempName[0]);
			field = drawable.getField(tempName[0]);
			r_id = field.getInt(field.getName());
		} catch (Exception e) {
			r_id = R.drawable.product_default;
			Log.e("ERROR", "PICTURE NOT FOUND!");
		}
		return r_id;
	}

}
